//
//  MapViewController.m
//  AddressBook
//
//  Created by dllo on 15/11/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MapViewController.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
#import <BaiduMapAPI_Radar/BMKRadarComponent.h>

@interface MapViewController ()<BMKMapViewDelegate, BMKGeoCodeSearchDelegate>

@property (nonatomic, strong) BMKMapView *mapView;
@property (nonatomic, strong) BMKGeoCodeSearch *searcher;      // 检索
@property (nonatomic, strong) BMKLocationService *locService;  // 定位
//@property (nonatomic, strong) BMKUserLocation *result;       // 经纬度
@property (nonatomic, strong) BMKPointAnnotation *annotation;  // 大头针
@property (nonatomic, strong) NSString *str;

@end

@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.mapView = [[BMKMapView alloc]initWithFrame:self.view.frame];
    self.view = _mapView;
    
    // 创建大头针
    self.annotation = [[BMKPointAnnotation alloc] init];
    
    
    //初始化检索对象
    self.searcher =[[BMKGeoCodeSearch alloc]init];
    self.searcher.delegate = self;
    BMKGeoCodeSearchOption *geoCodeSearchOption = [[BMKGeoCodeSearchOption alloc]init];
    
    geoCodeSearchOption.address = self.location;
    BOOL flag = [self.searcher geoCode:geoCodeSearchOption];
    if(flag){
        NSLog(@"geo检索发送成功");
    } else {
        NSLog(@"geo检索发送失败");
    }
    
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.mapView addGestureRecognizer:longPress];
    
    
    
    
}

- (void)longPress:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateEnded) {
        // 获取点击的 坐标 -> 相对地图的视图坐标
        CGPoint location = [longPress locationInView:self.mapView];
        // 长按结束增加大头针
        _annotation.coordinate = [self.mapView convertPoint:location toCoordinateFromView:self.mapView];
        
        //发起反向地理编码检索
        CLLocationCoordinate2D pt = (CLLocationCoordinate2D){_annotation.coordinate.latitude, _annotation.coordinate.longitude};
        BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[
        BMKReverseGeoCodeOption alloc]init];
        reverseGeoCodeSearchOption.reverseGeoPoint = pt;
        BOOL flag = [_searcher reverseGeoCode:reverseGeoCodeSearchOption];
        
        if(flag)
        {
          NSLog(@"反geo检索发送成功");
            
        }
        else
        {
          NSLog(@"反geo检索发送失败");
        }
    }
    
}

//接收反向地理编码结果
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    
    if (error == BMK_SEARCH_NO_ERROR) {
      // 在此处理正常结果
        CLLocationCoordinate2D coor;
        coor.latitude = result.location.latitude;
        coor.longitude = result.location.longitude;
        _annotation.coordinate = coor;
        [self.mapView setCenterCoordinate:coor];
        // 实际上应该根据经纬度 进行地理反编码 得到地址作为大头针数据
      _annotation.title = [NSString stringWithFormat:@"这里是%@%@%@%@", result.addressDetail.province, result.addressDetail.city, result.addressDetail.district, result.addressDetail.streetName];
        self.str = [NSString stringWithFormat:@"%@%@%@%@", result.addressDetail.province, result.addressDetail.city, result.addressDetail.district, result.addressDetail.streetName];
        // 增加到地图上
        [_mapView addAnnotation:_annotation];
        
    }
    else {
      NSLog(@"抱歉，未找到结果");
    }
}


// 经纬度
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        
        
        
        // 添加一个PointAnnotation
        CLLocationCoordinate2D coor;
        coor.latitude = result.location.latitude;
        coor.longitude = result.location.longitude;
        _annotation.coordinate = coor;
        [self.mapView setCenterCoordinate:coor];
        _annotation.title = [NSString stringWithFormat:@"这里是%@", self.location];
            [_mapView addAnnotation:_annotation];
        
    }
    else {
        NSLog(@"抱歉，未找到结果");
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.str.length != 0) {
        
        [self.delegate takeValue:self.str];
    }
}






// 显示地图
//- (void)viewWillAppear:(BOOL)animated
//{
//    [_mapView viewWillAppear];
//    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
//}
//- (void)viewWillDisappear:(BOOL)animated
//{
//    [_mapView viewWillDisappear];
//    _mapView.delegate = nil; // 不用时，置nil
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
